/**
 * @Author：ming
 * @Date：2023/10/9 6:58 AM
 * @Filename：Lc1160
 */

import java.util.HashMap;

/**
 * @author ming
 */
public class Lc1160 {
    public int countCharacters(String[] words, String chars) {
        int len = words.length;
        int all = 0;

        char[] cc = chars.toCharArray();
        HashMap<Character, Integer> map = new HashMap<>();
        for (char c : cc) {
            if(map.containsKey(c)){
                map.put(c,map.get(c)+1);
            }else {
                map.put(c,1);
            }
        }

        for (int i = 0; i < words.length; i++) {
            HashMap<Character, Integer> temp = new HashMap<>();
            char[] c1 = words[i].toCharArray();
            for (char c : c1) {
                if(temp.containsKey(c)){
                    temp.put(c,temp.get(c)+1);
                }else {
                    temp.put(c,1);
                }
            }
            int count = temp.size();
            for (Character c :temp.keySet()) {
                if(map.containsKey(c)&&map.get(c)>=temp.get(c)){
                    count--;
                }else {
                    break;
                }
            }
            if(count==0){
                all += c1.length;
            }

        }
        return all;
    }
}
